package ${packageName}.service.impl;

import ${packageName}.model.entity.*;
import ${packageName}.model.vo.*;
import ${packageName}.model.dto.*;
import ${packageName}.mapper.${name}Mapper;
import ${packageName}.service.${name}Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.apache.ibatis.session.RowBounds;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import tk.mybatis.mapper.entity.Example;
import org.springframework.beans.BeanUtils;
import java.util.*;

/**
* Copyright (c) 2018-2025, chris All rights reserved.
* <p>
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* <p>
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* Neither the name of the pig4cloud.com developer nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
* <p>
* Author: ${author}
* <p>
* Date: ${time}
* ${description}
**/
@Service
public class ${name}ServiceImpl implements ${name}Service {
    @Autowired
    private ${name}Mapper ${lowerName}Mapper;

    public Page<${name}> query(Pageable page) {
        Example e = new Example(${name}.class);
        Example.Criteria criteria = e.createCriteria();
        RowBounds rowBounds = new RowBounds(Long.valueOf(page.getOffset()).intValue(), page.getPageSize());
        List<${name}> list = ${lowerName}Mapper.selectByExampleAndRowBounds(e, rowBounds);
        int total = ${lowerName}Mapper.selectCountByExample(e);
        return new PageImpl<>(list, page, total);
    }

    public ${name} get(${primaryFieldType} id) {
        if (id == null) {
            return null;
        }
        return ${lowerName}Mapper.selectByPrimaryKey(id);
    }

    public ${name} add(${name}DTO ${lowerName}) {
        ${name} u = new ${name}();
        BeanUtils.copyProperties(${lowerName}, u);
        u.setModifyDate(new Date());
        u.setCreateDate(new Date());
        ${lowerName}Mapper.insert(u);
        return u;
    }

    public void update(${name}DTO ${lowerName}) {
        if (${lowerName}.get${primaryFieldNameUpper}() == null) {
            return;
        }
        ${name} u = new ${name}();
        BeanUtils.copyProperties(${lowerName}, u);
        u.setModifyDate(new Date());
        ${lowerName}Mapper.updateByPrimaryKeySelective(u);
    }

    public void delete(${primaryFieldType} id) {
        if (id == null) {
            return;
        }
        ${lowerName}Mapper.deleteByPrimaryKey(id);
    }
}
